-- way simple particles

particle = 
{
	img = nil, -- image
	age = 1, -- dies on zero
	tint_r = 255, tint_g = 255, tint_b = 255, -- tint
	
	pos = vec2:new(),
	vel = vec2:new(),
	
	ang = 0,
	sx = 1, sy = 1,
	fade = 1,
}

part_tex = 
{
	["bubble"] = love.graphics.newImage("images/Bubble_01/Bubble_00.png"),
	["ink"] = love.graphics.newImage("images/Ink_01/Ink_00.png"),
}

function particle:new(o)
	o = o or {}
	setmetatable(o, self)
	self.__index = self
	return o
end

function particle:update(dt)
	if self.think then
		self:think()
	end
	
	self.pos = self.pos + dt * self.vel
	
	self.age = self.age - dt
	
	if self.age < 0 then
		self.todelete = true
	end
end

function particle:draw()
	love.graphics.setColor(self.tint_r, self.tint_g, self.tint_b, 255 * self.fade)
	love.graphics.draw(self.img, self.pos.x, self.pos.y, self.ang, self.sx, self.sy, self.img:getWidth()/2, self.img:getHeight()/2)
end

function particle:newBubble(pos)
	local r = particle:new()
	self.img = part_tex["bubble"]
	r.pos = pos + vec2:init(math.random(-20, 20), math.random(-20, 20))
	r.vel = vec2:init(0, 20)
	r.age = 5
	
	r.speed = 60
	r.mean = rad(90)
	r.range = rad(45)
	r.phase = math.random(2 * math.pi)
	
	function r:think()
		local s = math.sin(4 * self.age + self.phase)
		self.vel = self.speed * vec2:dir(self.mean + s * self.range)
		--self.ang = ang_norm(self.mean + s * self.range)
		
		if self.age < 1 then
			self.fade = self.age
		end
	end
	
	table.insert(obj, r)
	
	return r
end

function particle:newPoints(pos, points)
	local r = particle:new()
	
	r.img = part_tex["ink"]
	r.pos = pos + vec2:init(math.random(-20, 20), math.random(-20, 20))
	r.vel = vec2:init(0, 20)
	r.age = 5
	r.fade = 1
	
	r.speed = 90
	r.mean = rad(90)
	r.range = rad(45)
	r.phase = math.random(2 * math.pi)
	r.points = tostring(points)
	
	r.tint_r, r.tint_g, r.tint_b = 255, 0, 0
	
	function r:think()
		local s = math.sin(4 * self.age + self.phase)
		self.vel = self.speed * vec2:dir(self.mean + s * self.range)
		--self.ang = ang_norm(self.mean + s * self.range)
		
		if self.age < 1 then
			self.fade = self.age
		end
	end
	
	function r:draw()
		love.graphics.setFont(font);
		love.graphics.setColor(self.tint_r, self.tint_g, self.tint_b)--, 255 * self.fade)
		love.graphics.print(self.points, self.pos.x, self.pos.y)
	end
	
	table.insert(obj, r)
	
	return r
end
